/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gridworld_v04;

import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JFrame;

/**
 *
 * @author Jonah
 */
public class Game extends JFrame{
    /*implementation comment*/
    ////<editor-fold defaultstate="collapsed" desc="Static Variables">
    //</editor-fold>
    ////<editor-fold desc="Instanced Variables">
    private TileEnum placingState = TileEnum.Default;
    private Board board;

    public Board getBoard() {
        return board;
    }

    public void setBoard(Board board) {
        this.board = board;
    }
    private UserSettings us;
    private Queue<Actor> actors = new LinkedList();
    //these public variablea re what is checked for actor behavior
    public int bugStartingLifespan = 10, bugEatsflower = 6, bugReproduction =14, bugRproductionCost = 7 , bugHunger = 12, flowerLifespan = 6,critterEatsBug = 6, critterLifespan = 6, critterReproductiveSpan = 30;
    public int critterBirth = 12, bugSeedTime = 4, adjacentBugsToKillCritter = 5, critterReproductiveCost = 4;
    public double chanceBugWillEatSeed = 0.333;
    IteratorThread iter;
    //<editor-fold defaultstate="collapsed" desc="get and set methods">
    public TileEnum getPlacingState() {
        return placingState;
    }
    public void setPlacingState(TileEnum placingState) {
        this.placingState = placingState;
    }
    //</editor-fold>
    //</editor-fold>
    
    ////<editor-fold defaultstate="collapsed" desc="Constructor">
    public Game(int pixels, int rows, int columns) {
        board = new Board(pixels, rows, columns, this);
        us = new UserSettings(this, board);
        actors.add(new Rock(null, this, board));//this is  holder rock for checking turns end
        
        //swing intializations
        setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
        //setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        add(board);
        add(us);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        pack();
    }
    //</editor-fold>
    ////<editor-fold desc="Class Methods">
    public void generateActors(Integer rocks,Integer flowers,Integer critters,Integer bugs ){
        for( int i = 0; i < rocks; i++){
            createActorInTile(TileEnum.Rock, getRandEmptyTile());
        }
        for( int i = 0; i < flowers; i++){
            createActorInTile(TileEnum.Flower, getRandEmptyTile());
        }
        for( int i = 0; i < critters; i++){
            createActorInTile(TileEnum.Critter, getRandEmptyTile());
        }
        for( int i = 0; i < bugs; i++){
            createActorInTile(TileEnum.Bug, getRandEmptyTile());
        }
    }
    
    private Tile getRandEmptyTile(){
        int tileIndex = (int)(Math.random()*board.getNumberOfTiles());
        if (board.getTile(tileIndex).getOccupants().isEmpty()){
            return board.getTile(tileIndex);
        }else return getRandEmptyTile();
    }
    public void processTileClick(Tile tile) {
        createActorInTile(placingState, tile);
    }
    public void createActorInTile(TileEnum type, Tile tile){
        switch(type){
            case Rock:
                if (!tile.checkOccupantType(type, tile)){
                    tile.addOccupant(new Rock(tile, this, board));
                }
                break;
            case Flower:
                if (!tile.checkOccupantType(type, tile)){
                    tile.addOccupant(new Flower(tile, this, board, flowerLifespan));
                }
                break;
            case Critter:
                if (!tile.checkOccupantType(type, tile)){
                    tile.addOccupant(new Critter(tile, this, board, critterLifespan));
                }
                break;
            case Bug:
                if (!tile.checkOccupantType(type, tile)){
                    tile.addOccupant(new Bug(tile, this, board, bugStartingLifespan, bugHunger));
                }
                break;
            case Invalid:
            case Default:
        }
    }
    public void addActorReference(Actor actor) {
        actors.add(actor);
    }
    public void removeActorReference(Actor actor) {
        Tile removed = actor.getCurrentTile();
        actors.remove(actor);
        //removed.processOccupants();
    }
    public void iterateXturns(int turns, long wait){
        if (turns < 1){
            return;
        }
        iter = new IteratorThread(this, wait, turns);
        iter.start();
        //System.out.println("")
    }
        
    public void iterateTurn() {
        Actor first = actors.peek();
        if (first == null){
            System.err.println("no actors in game");
            return;
        }
        do{
        if(actors.peek()==null)break;
        Actor current = actors.poll();
        current.move();
        //actors.add(current);
        }while((actors.peek()!=first)&&(!first.isDead()));
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Interface Override Methods">
    //</editor-fold>


}
